/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.antColonyOptimisation.ant.pathFinder;

import org.utc.ia04.antsontheweb.httprequest.agent.HttpReqPoolManager;
import org.utc.ia04.antsontheweb.utilities.Requete;
import org.utc.ia04.antsontheweb.utilities.RequeteException;

import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

public class GetParserAIDBhv extends OneShotBehaviour
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6035893065563535392L;

	public void action()
	{
		boolean end = false;
		while(!end)
		{
			ACLMessage msg = myAgent.blockingReceive();
			if(msg == null) break;
			else
			{
				String msgContent = msg.getContent();
				try
				{
					Requete query = Requete.deserialize_withException(msgContent, myAgent, msg);
					switch(query.getType())
					{
						case Requete.ANT_SENDPARSERAID:
							((AntPathFinderAgent)myAgent).setParserAID(new AID(query.getContent(),true));
							end = true;
								
							//Attention action de lancement du systeme 
							ACLMessage httpRequest = new ACLMessage(ACLMessage.REQUEST);
							Requete sendQuery = new Requete(Requete.ANTPATHFINDER_ASKHTMLCONTENT,((AntPathFinderAgent)myAgent).getAntColonyUrl().toString());
							httpRequest.setContent(sendQuery.serialize());
							ServiceDescription sd = new ServiceDescription();
							DFAgentDescription template = new DFAgentDescription();
							sd.setType(HttpReqPoolManager.DF_TYPE);
							template.addServices(sd);
							try
							{
								DFAgentDescription[] result = DFService.search(myAgent, template);
								int nbAgent = result.length; int selection = (int) (Math.random()*nbAgent);
								if(nbAgent > 0) httpRequest.addReceiver(result[selection].getName());
							}
							catch(Exception fe) {System.out.println(fe.getMessage());}
							httpRequest.addReplyTo(((AntPathFinderAgent)myAgent).getParserAID());
							myAgent.send(httpRequest);
							break;
						default:
							System.out.println(myAgent.getName()+"received message:"+msgContent+"and not know how to treat it.");
							break;
					}
				}
				catch (RequeteException e) { }
			}
		}
		
		myAgent.addBehaviour(new AntPathFinderBhv());
		
	}
	
}
